首先画出分析图
实现效果如图
项目下的包如图:
实体包
package com.contactSystem.entiey;public class Contact { private String Id; private String name; private String sex; private String age; private String phone; private String qq; private String email; public String getId() { return Id; } public void setId(String id) { Id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Contact [Id=" + Id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", phone=" + phone + ", qq=" + qq + ", email=" + email + "]"; }}
XML的工具包(只是避免了代码的重复使用,将其放进工具包中)
package com.contactSystem.util;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.OutputStream;import javax.management.RuntimeErrorException;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;/* * xml操作的工具类 */public class XMLUtil { //写出一个xml文件 public static void write2xml(Document doc) throws Exception{ OutputStream out=new FileOutputStream("e:/contact.xml"); OutputFormat format=OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter writer=new XMLWriter(out,format); writer.write(doc); writer.close(); } //读取本地xml文件的方法 public static Document getDocument(){ Document doc; try { doc = new SAXReader().read("e:/contact.xml"); return doc; } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } } }
抽象的接口
package com.contactSystem.dao;import java.io.FileNotFoundException;import java.io.UnsupportedEncodingException;import java.util.List;import org.dom4j.DocumentException;import com.contactSystem.entiey.Contact;public interface ContactOperate { public void addContact(Contact contact) throws Exception; public void updateContact(Contact contact) throws Exception; public void removeContact(String id) throws Exception; public Contact findContact(String id) throws Exception; public ListallContacts();}
具体的实现方法和操作
package com.contactSystem.dao.daoImpl;import java.io.File;import java.util.ArrayList;import java.util.List;import java.util.UUID;import javax.persistence.Id;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.SAXReader;import com.contactSystem.dao.ContactOperate;import com.contactSystem.entiey.Contact;import com.contactSystem.util.XMLUtil;public class Operater implements ContactOperate { @Override //添加联系人 public void addContact(Contact contact) throws Exception { // TODO Auto-generated method stub File file=new File("e:/contact.xml"); Document doc=null; Element rootElem=null; if (file.exists()) { doc=new SAXReader().read(file); rootElem=doc.getRootElement(); }else { doc=DocumentHelper.createDocument(); rootElem=doc.addElement("ContactList"); } //开始添加个体 Element element=rootElem.addElement("contact"); //有系统自动生成一随机且唯一的ID,赋给联系人Id,系统提供了一个包UUID包 String uuid=UUID.randomUUID().toString().replace("-", ""); element.addAttribute("Id", uuid); element.addElement("姓名").setText(contact.getName()); element.addElement("name").setText(contact.getName()); element.addElement("sex").setText(contact.getSex()); element.addElement("age").setText(contact.getAge()); element.addElement("phone").setText(contact.getPhone()); element.addElement("email").setText(contact.getEmail()); element.addElement("qq").setText(contact.getQq()); //写入到本地的xml文档中 XMLUtil.write2xml(doc); } @Override public void updateContact(Contact contact) throws Exception { // TODO Auto-generated method stub //通过xpath查找对应id的联系人 Document document=XMLUtil.getDocument(); Element element=(Element) document.selectSingleNode("//contact[@Id='"+contact.getId()+"']"); //根据标签改文本 System.out.println(element); element.element("name").setText(contact.getName()); element.element("age").setText(contact.getAge()); element.element("email").setText(contact.getEmail()); element.element("phone").setText(contact.getPhone()); element.element("sex").setText(contact.getSex()); element.element("qq").setText(contact.getQq()); XMLUtil.write2xml(document); } @Override public void removeContact(String id) throws Exception { // TODO Auto-generated method stub //通过xpath去查找对应的contact Document document=XMLUtil.getDocument(); Element element=(Element) document.selectSingleNode("//contact[@Id='"+id+"']"); /** * 如果是火狐浏览器,其本身有一个bug,对于一个get请求,它会重复做两次, * 第一次会把一个唯一的id给删除掉,但是在第二次的时候,它会继续去删,而这个时候查找不到,会报错,会发生错误, * 为了解决这个bug,我们在这里验证其是否为空 */ if (element!=null) { element.detach(); XMLUtil.write2xml(document); } } @Override public Contact findContact(String id) throws Exception { // TODO Auto-generated method stub Document document=XMLUtil.getDocument(); Element e=(Element) document.selectSingleNode("//contact[@Id='"+id+"']"); Contact contact=null; if (e!=null) { contact=new Contact(); contact.setAge(e.elementText("age")); contact.setEmail(e.elementText("email")); contact.setId(e.attributeValue("id")); contact.setName(e.elementText("name")); contact.setPhone(e.elementText("phone")); contact.setSex(e.elementText("sex")); contact.setQq(e.elementText("qq")); } return contact; } @Override public ListallContacts() { // TODO Auto-generated method stub Document document=XMLUtil.getDocument(); List list=new ArrayList (); List conElements=(List )document.selectNodes("//contact"); for (Element element : conElements) { Contact contact=new Contact(); contact.setId(element.attributeValue("Id")); contact.setAge(element.elementText("age")); contact.setEmail(element.elementText("email")); contact.setName(element.elementText("name")); contact.setPhone(element.elementText("phone")); contact.setQq(element.elementText("qq")); contact.setSex(element.elementText("sex")); list.add(contact); } return list; }}
添加联系人的html页面
添加联系人 添加联系人
接下来则是最重要的servlet(显示首页,所有联系人)
package com.contactSystem.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.contactSystem.dao.daoImpl.Operater;import com.contactSystem.entiey.Contact;public class Index extends HttpServlet { /** * 显示所有联系人的逻辑方式 */ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); Operater operater=new Operater(); Listcontacts=operater.allContacts(); PrintWriter writer=response.getWriter(); /** * shift+alt+A ——>区域选择 * 正则表达式:“."表示任意字符,"*"表示多个字符 * “/1”表示一行代表匹配一行内容 */ String html=""; html+= ""; html+= ""; html+= ""; html+= ""; html+= " "; html+= " 查询所有联系人 "; html+= " "; html+= ""; html+= ""; html+= ""; html+= ""; html+= " "; html+= "查询所有联系人
"; html+= ""; html+= " "; html+= " "; html+= "
"; html+= ""; html+= ""; html+= ""; writer.write(html); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}"; html+= " "; html+= " 编号 "; html+= "姓名 "; html+= "性别 "; html+= "年龄 "; html+= "电话 "; html+= "邮箱 "; html+= "操作 "; html+= ""; if (contacts!=null) { for (Contact contact : contacts) { html+= " "; } } html+= ""+contact.getId()+" "; html+= ""+contact.getName()+" "; html+= ""+contact.getSex()+" "; html+= ""+contact.getAge()+" "; html+= ""+contact.getPhone()+" "; html+= ""+contact.getQq()+" "; html+= ""+contact.getEmail()+" "; html+= "修改 删除 "; html+= ""; html+= " "; html+= " "; html+= ""; html+= " 添加联系人"; html+= " "; html+= "
添加联系人的servlet
package com.contactSystem.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.contactSystem.dao.daoImpl.Operater;import com.contactSystem.entiey.Contact;public class addServlet extends HttpServlet { /** * 处理添加联系人的逻辑 */ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String userName=request.getParameter("userName"); String age=request.getParameter("age"); String sex=request.getParameter("sex"); String phone=request.getParameter("phone"); String qq=request.getParameter("qq"); String email=request.getParameter("email"); Operater operater=new Operater(); Contact contact=new Contact(); contact.setName(userName); contact.setAge(age); contact.setSex(sex); contact.setPhone(phone); contact.setQq(qq); contact.setEmail(email); try { operater.addContact(contact); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } response.sendRedirect(request.getContextPath()+"/Index"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}
修改联系人,根据查找到的联系人,显示在其上面如图(根据显示的首页显示得内容为本页面的默认值)
首先是要去查找这个联系人的信息,即FindIdServlet
package com.contactSystem.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.contactSystem.dao.daoImpl.Operater;import com.contactSystem.entiey.Contact;public class FindIdServlet extends HttpServlet { /** * 修改联系人逻辑 */ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter writer=response.getWriter(); Operater operater=new Operater(); String id=request.getParameter("id"); Contact contact=null; try { contact=operater.findContact(id); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } String html=""; html += ""; html += ""; html += ""; html += " "; html += ""; html += ""; html += ""; html += ""; writer.write(html); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}添加联系人 "; html += " "; html += ""; html += ""; html += ""; html += ""; html += " "; html += "修改联系人
"; html += "
然后则是根据提交的内容去修改本地的联系人
package com.contactSystem.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.contactSystem.dao.daoImpl.Operater;import com.contactSystem.entiey.Contact;public class UpdateServlet extends HttpServlet { /** * 将修改后的数据提交 */ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); request.setCharacterEncoding("utf-8"); String userName=request.getParameter("userName"); System.out.println(userName); String age=request.getParameter("age"); String sex=request.getParameter("sex"); String phone=request.getParameter("phone"); String qq=request.getParameter("qq"); String email=request.getParameter("email"); String id=request.getParameter("id"); Operater operater=new Operater(); Contact contact=new Contact(); contact.setId(id); contact.setName(userName); contact.setAge(age); contact.setSex(sex); contact.setPhone(phone); contact.setQq(qq); contact.setEmail(email); try { operater.updateContact(contact); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } response.sendRedirect(request.getContextPath()+"/Index"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}
最后则是删除联系人,不过要注意的是:火狐浏览器如果判断是get请求的话,会向服务器发送两次请求,可能会导致一些问题,其他的浏览器不会出出现该问题
package com.contactSystem.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.contactSystem.dao.daoImpl.Operater;public class DeleteServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); String id=request.getParameter("id"); Operater operater=new Operater(); try { operater.removeContact(id); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } response.sendRedirect(request.getContextPath()+"/Index"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}
最后:不用jsp去写,就用servlet去写html真的好累,还是好好学jsp吧。